//获取应用实例
const app = getApp();
const umfDialog = require('../../../utils/UmfDialog.js');
const constants = require('../../../common/Constants.js');
const network_utils = require('../../../http/NetworkUtils.js');
const regexUtils = require('../../../utils/RegexUtils.js');
const utils = require('../../../utils/UmfUtils');

import { $Toptips } from '../../../components/CompRouter';

Page({

    onLoad: function (options) {
        this.setData({
            agentId: app.globalData.userData.assistId,
            userId: app.globalData.userData.userId,
            agentName: app.globalData.userData.agentName,
        });
    },

    onPwdInput: function (ee) {
        //密码特殊处理，不允许输入空格
        let value = ee.detail.value;
        if (value.indexOf(" ") < 0) {
            return value;
        }
        return utils.removeSpace(value);
    },

    onSubmit: function (e) {
        //密码校验
        let newPwd = e.detail.value.pwdInput1;
        let newPwdConfirm = e.detail.value.pwdInput2;
        if (this._check_pwd(newPwd, newPwdConfirm)) {
            //网络请求
            this._requestChangePwd(newPwd);
        }
    },

    _check_pwd: function (newPwd, newPwdConfirm) {
        if (newPwd !== newPwdConfirm) {
            $Toptips.error({text: "两次输入的密码不一致"});
            return false;
        }
        let errorMsg = regexUtils.regexTypeWithMsg(newPwd, constants.RegexTypes.NEW_LOGIN_PWD);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
            return false;
        }
        return true;
    },

    _requestChangePwd: function (newPwd) {
        let params = {
            password: newPwd
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_MESS_MODIFIY, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    umfDialog.show("保存修改成功", ()=> {
                        wx.navigateBack({delta: 1});
                    });
                }
                return false;
            }
        }).send();
    }
});